﻿CREATE PROC [dbo].[usp_ElectricityMonthly_AutoInsert] 
    @am_month datetime,
    @am_kWh decimal(18, 3),
    @am_meterId int
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	
		IF NOT EXISTS (SELECT * FROM [ElectricityMonthly_Auto] WHERE [am_month] = @am_month AND [am_meterId] =@am_meterId )
	
		BEGIN
			PRINT 'NO DATA, INSERTING'		
			INSERT INTO [dbo].[ElectricityMonthly_Auto] ([am_month], [am_kWh], [am_meterId])
			SELECT @am_month, @am_kWh, @am_meterId
			
			-- Begin Return Select <- do not remove
			SELECT [am_id], [am_month], [am_kWh], [am_meterId]
			FROM   [dbo].[ElectricityMonthly_Auto]
			WHERE  [am_id] = SCOPE_IDENTITY()
		END
	ELSE
		BEGIN
			PRINT 'RECORD ALREADY EXISTS'	
			SELECT * FROM [ElectricityMonthly_Auto] WHERE [am_month] = @am_month AND [am_meterId] =@am_meterId 
		END
			-- End Return Select <- do not remove


